package com.hanson.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.util.PageInfo;
import com.hanson.entity.GoodsRuleBean;
import com.hanson.service.GoodsRuleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@RequestMapping("/admin/goodsRule")
public class GoodsRuleController {
    @Autowired
    private GoodsRuleService goodsRuleService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String login(ModelMap modelMap, String pid) {
        return "admin/goods/goodsRule/index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap modelMap, String id) {
        if (StringUtils.isNotEmpty(id)) {
            modelMap.put("data", JSON.toJSONString(goodsRuleService.detailGoodsRule(Integer.parseInt(id))));
        }
        return "admin/goods/goodsRule/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage add(@RequestBody GoodsRuleBean goodsRuleBean) {
        System.out.println(goodsRuleBean);
        ResultMessage resultMessage = new ResultMessage();
        goodsRuleService.addGoodsRule(goodsRuleBean);
        resultMessage.setMsg("新增商品规格模板成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage modify(@RequestBody GoodsRuleBean goodsRuleBean) {
        ResultMessage resultMessage = new ResultMessage();
        goodsRuleService.modifyGoodsRule(goodsRuleBean);
        resultMessage.setMsg("修改商品规格模板成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage delete(String id) {
        ResultMessage resultMessage = new ResultMessage();
        goodsRuleService.deleteGoodsRule(Integer.parseInt(id));
        resultMessage.setMsg("删除商品规格模板成功！");
        return resultMessage;
    }

    @RequestMapping(value = "listData", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<GoodsRuleBean> listData(int limit, int page, GoodsRuleBean goodsRuleBean) {
        Page<GoodsRuleBean> pageInfo = new Page<>(page,limit);
        return new PageInfo(goodsRuleService.listGoodsRule(pageInfo, goodsRuleBean));
    }
}
